package com.starter.Controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.starter.model.User;
import com.starter.service.UserService;
import lombok.extern.log4j.Log4j;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2017/12/7.
 */
@RestController
@RequestMapping(value = "/user")
@Log4j
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/add", produces = {"application/json;charset=UTF-8"})
    public int addUser(User user){
        return userService.addUser(user);
    }

    @RequestMapping(value = "/all/{pageNum}/{pageSize}")
    public Map findAllUser(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize){
        log.info("查询所有数据");
        Map re = new HashMap();
        re.put("dataList",userService.findAllUser(pageNum,pageSize));
        re.put("dataTotal",userService.selectAllUserTotal());
        return re;
        /*try {
           *//* response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            String resultJSON = JSONArray.toJSONString(userService.findAllUser(pageNum,pageSize));
            out.println(jsonpCallback + "(" + resultJSON + ")");//返回jsonp格式数据
            out.flush();
            out.close();*//*

        }catch (Exception e){
            log.info(e.getMessage());
        }*/
    }
    @RequestMapping(value = "/hello")
    public String hello(){
        System.out.print("haha啊哈哈");
        return "index";
    }
}
